package org.jeecg.modules.assets.controller;

import java.util.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.thread.ThreadUtil;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.assets.dto.StartAssetsDoubleQuotaionDTO;
import org.jeecg.modules.assets.entity.AssetsDoubleQuotationResult;
import org.jeecg.modules.assets.service.IAssetsDoubleQuotationResultService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 复盘管理
 * @Author: jeecg-boot
 * @Date: 2023-12-20
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "复盘管理")
@RestController
@RequestMapping("/assets/assetsDoubleQuotationResult")
public class AssetsDoubleQuotationResultController extends JeecgController<AssetsDoubleQuotationResult, IAssetsDoubleQuotationResultService> {
    @Autowired
    private IAssetsDoubleQuotationResultService assetsDoubleQuotationResultService;

    /**
     * 分页列表查询
     *
     * @param assetsDoubleQuotationResult
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "复盘管理-分页列表查询")
    @ApiOperation(value = "复盘管理-分页列表查询", notes = "复盘管理-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(AssetsDoubleQuotationResult assetsDoubleQuotationResult,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {

        Page<AssetsDoubleQuotationResult> page = new Page<AssetsDoubleQuotationResult>(pageNo, pageSize);
        String startTime = assetsDoubleQuotationResult.getStartTime();
        assetsDoubleQuotationResult.setStartTime(null);
        QueryWrapper<AssetsDoubleQuotationResult> queryWrapper = QueryGenerator.initQueryWrapper(assetsDoubleQuotationResult, req.getParameterMap());
        if (StringUtils.isNotEmpty(startTime)) {
            queryWrapper.eq("DATE_FORMAT(start_time, '%Y-%m')", startTime);
        }
        //按创建时间倒序
        if(StringUtils.isNotEmpty( assetsDoubleQuotationResult.getCompany())){
            queryWrapper.exists("SELECT 1 from  assets_double_quotation_result_item b WHERE  assets_double_quotation_result.id =b.apply_id and b.company={0}",assetsDoubleQuotationResult.getCompany());
        }
        queryWrapper.orderByDesc("create_time");
        IPage<AssetsDoubleQuotationResult> pageList = assetsDoubleQuotationResultService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param assetsDoubleQuotationResult
     * @return
     */
    @AutoLog(value = "复盘管理-添加")
    @ApiOperation(value = "复盘管理-添加", notes = "复盘管理-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody AssetsDoubleQuotationResult assetsDoubleQuotationResult) {
        assetsDoubleQuotationResultService.save(assetsDoubleQuotationResult);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param assetsDoubleQuotationResult
     * @return
     */
    @AutoLog(value = "复盘管理-编辑")
    @ApiOperation(value = "复盘管理-编辑", notes = "复盘管理-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<?> edit(@RequestBody AssetsDoubleQuotationResult assetsDoubleQuotationResult) {
        assetsDoubleQuotationResultService.updateById(assetsDoubleQuotationResult);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "复盘管理-通过id删除")
    @ApiOperation(value = "复盘管理-通过id删除", notes = "复盘管理-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        assetsDoubleQuotationResultService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "复盘管理-批量删除")
    @ApiOperation(value = "复盘管理-批量删除", notes = "复盘管理-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.assetsDoubleQuotationResultService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "复盘管理-通过id查询")
    @ApiOperation(value = "复盘管理-通过id查询", notes = "复盘管理-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        AssetsDoubleQuotationResult assetsDoubleQuotationResult = assetsDoubleQuotationResultService.getById(id);
        return Result.OK(assetsDoubleQuotationResult);
    }

    @AutoLog(value = "复盘管理-创建")
    @ApiOperation(value = "复盘管理-创建", notes = "复盘管理-创建")
    @PostMapping(value = "/startAssets")
    public Result<?> startAssets(@RequestBody StartAssetsDoubleQuotaionDTO startAssets) {

        String endTime = startAssets.getEndTime();
        DateTime endDate = DateUtil.parse(endTime, "yyyy-MM-dd HH:mm:ss");
        //当前时间加一天
        Calendar calendar = DateUtil.date(new Date()).toCalendar();
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        Date time = calendar.getTime();
        if (!endDate.isAfter(time)) {
            return Result.error("结束时间需大于当前时间24小时");

        }
        ThreadUtil.execAsync(() -> assetsDoubleQuotationResultService.startAssetsAdmin(startAssets));

        return Result.OK("添加成功！");
    }

    /**
     * 导出excel
     *
     * @param request
     * @param assetsDoubleQuotationResult
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AssetsDoubleQuotationResult assetsDoubleQuotationResult) {
        return super.exportXls(request, assetsDoubleQuotationResult, AssetsDoubleQuotationResult.class, "复盘管理");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, AssetsDoubleQuotationResult.class);
    }

}
